package com.lz.gmall.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lz.gmall.common.result.Result;
import com.lz.gmall.order.dto.OrderSubmitDTO;
import com.lz.gmall.order.entity.OrderInfo;
import com.lz.gmall.order.entity.WareStockMsg;
import com.lz.gmall.order.service.OrderInfoService;
import com.lz.gmall.order.vo.OrderDetailListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author 郭运龙
 */
@RestController
@RequestMapping("/api/order")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 生成订单，把订单存入数据库
     * @param tradeNo  订单编号
     * @param orderSubmitDTO 订单详情数据
     * @return
     */
    @PostMapping("/auth/submitOrder")
    public Result<String> submitOrder(String tradeNo, @Valid @RequestBody OrderSubmitDTO orderSubmitDTO){
        String orderId = orderInfoService.submitOrder(tradeNo,orderSubmitDTO);
        return Result.ok(orderId);
    }

    /**
     * 在支付成功之后，进行对订单的拆分，该项目的特色
     * 1、针对不同的订单跟踪不同的物流信息
     * 2、方便商家查看自己的订单数据
     * 请求参数：.....orderSplit?orderId=xxx&wareSkuMap=xxx
     * @return
     */
    @PostMapping("/orderSplit")
    public List<WareStockMsg> orderSplit(@RequestParam("orderId") Long orderId, @RequestParam("wareSkuMap") String wareSkuMap){
        return orderInfoService.orderSplit(orderId,wareSkuMap);
    }

    /**
     * 查询所用订单（拆表后的小订单）
     * @param pageNum
     * @param limit
     * @return
     */
    @GetMapping("/auth/{page}/{limit}")
    public Result getOrderPageList(@PathVariable("page")Integer pageNum,@PathVariable("limit")Integer limit){
        Page orderInfoPage = orderInfoService.getOrderPageList(pageNum,limit);
        return Result.ok(orderInfoPage);
    }
}
